/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * callbacks.c
 * Copyright (C) chaitanya chandel 2009 <cchandel@yahoo.com>
 * 
 * callbacks.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * callbacks.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <Elementary.h>
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include "dbus-stuff.h"
#include "gui.h"

//globals
int fd, no_songs = 0, song_index = 0, vol = 80;
char cur_playlist[255];
pthread_t thread_id;
Elm_Genlist_Item *song_list[10000];
int play_state = IDLE;
Ecore_Timer *timer1 = NULL;
int pos_time = 0, seek = 0, song_dur = 0;
FILE *mp_data;
_dir dir_gl_entry[255];

static void
my_win_del(void *data, Evas_Object *obj, void *event_info)
{
	int ret;

	//sort out closing other window too
	//tell mplayer to quit
	ret = write(fd, "quit 1\n", 7);
	//TODO : read mplayer process output to confirm it has shut down

	//clear the list
	elm_genlist_clear(list);
	//save_state();
	
	//close the files
	close(fd);
	fclose(mp_data);
	
	sqlite3_close(songs);
	elm_exit();
}

void start_mplayer(unsigned int wid)
{
	int ret;
	char tmpstr[PATH_MAX];
	sprintf(tmpstr, "mplayer -vo glamo -wid 0x%x -afm ffmpeg -idle -quiet -slave -msglevel global=6 -input file=/tmp/intone-mplayer 1>/tmp/mplayer-intone", wid);
	ret = system(tmpstr);
}

EAPI int
elm_main(int argc, char **argv)
{
	Evas_Object *win, *inwin;
	int ret;

	//set intone-video directory
	sprintf(intone_dir, "/usr/share/intone-video");
	//get the users home directory
	const char *name = "HOME";
	sprintf(home_dir, "%s", getenv(name));

	occupy_display ();
	
	//set up win
	win = elm_win_add(NULL, "intonevideo", ELM_WIN_BASIC);
	elm_win_title_set(win, "Intone Video");
	evas_object_smart_callback_add(win, "delete-request", my_win_del, NULL);
	
	// show the window
	create_gui(win);
	evas_object_show(win);

	//show the sub win for mplayer
	inwin = elm_win_add(win, "video", ELM_WIN_BASIC);
	elm_win_override_set(inwin, 1); 
	//evas_object_show(inwin);
	//evas_object_resize(inwin, 390, 600);
	//evas_object_move(inwin, 500, 500);
	elm_win_borderless_set(inwin, 1);
	
	Ecore_X_Window xwin = elm_win_xwindow_get(inwin);
	printf("inwin 0x%x\n", xwin);
	
	//load snd-pcm-oss
	ret = system("modprobe snd-pcm-oss");
	
	//unlink the fifo files
	unlink("/tmp/intone-mplayer");
	unlink("/tmp/mplayer-intone");
	
	//create a fifo file for sending commands to mplayer
	mkfifo("/tmp/intone-mplayer", S_IRUSR| S_IWUSR | O_NONBLOCK);
	
	//create a fifo file for reading mplayer output
	mkfifo("/tmp/mplayer-intone", S_IRUSR| S_IWUSR | O_NONBLOCK);

	//create mplayer thread
	ret = pthread_create(&thread_id, NULL, (void *)start_mplayer, (void *)xwin);
	
	//open fifo read only
	mp_data = fopen("/tmp/mplayer-intone", "r");

	//open fifo write only
	fd = open("/tmp/intone-mplayer", O_WRONLY, 0666);
		
	//occupy_cpu();
	
	elm_run();
	//clean up stuff
	clean_up();
	ecore_main_loop_quit();
	elm_shutdown();
	
	return 0;
}
ELM_MAIN()
